package com.yzyz.utils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletContext;

public class Config {
	
	public static String PICTURE_COUNT = "PICTURE_COUNT";
	
	public static String PPT_IMAGES = "PPT_IMAGES";//幻灯片
	
	public static String ROUTE_BOARDS = "ROUTE_BOARDS";//行程板块
	
	public static String SCENIC_BOARDS = "SCENIC_BOARDS";//景区板块
	
	private static Properties properties = null;
	
	
	public static String getConfig(String key){
		return properties.getProperty(key);
	}
	
	public static void init(ServletContext servletContext){
		properties = null;
		File file = new File(servletContext.getRealPath("/")+"WEB-INF/yzyz.properties");
		InputStream input = null;
		InputStreamReader ir = null;
		BufferedReader reader = null;
		properties = new Properties();
		try {
			input = new FileInputStream(file);
			ir = new InputStreamReader(input,"utf8");
			reader = new BufferedReader(ir);
			properties.load(reader);
			printConfigurations();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(input != null){
				try {
					input.close();
				} catch (IOException e) {
					e.printStackTrace();
					System.exit(-1);
				}finally{
					if(ir != null){
						try {
							ir.close();
						} catch (IOException e) {
							e.printStackTrace();
							System.exit(-1);
						}finally{
							if(reader != null){
								try {
									reader.close();
								} catch (IOException e) {
									e.printStackTrace();
									System.exit(-1);
								}
							}
						}
					}
				}
			}
		}
	}
	public static void printConfigurations(){
		Set<Map.Entry<Object, Object>> kvs = properties.entrySet();
		for(Map.Entry<Object, Object> kv : kvs){
			System.out.println("             "+kv.getKey()+"="+kv.getValue());
		}
	}
}
